package cn.itsource.shop.service.impl;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.shop.domain.TShop;
import cn.itsource.shop.dto.ShopDto;
import cn.itsource.shop.mapper.ShopMapper;
import cn.itsource.shop.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional //添加事务
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public AjaxResult register(ShopDto dto) {
        //验证店铺名是否存在
        //通过店铺名查找店铺
        TShop shop = shopMapper.findByName(dto.getShop().getName());
        //如果存在 重复
        if(shop!=null){
            return AjaxResult.me().setMsg("店铺名重复！请修改");
        }
        //先实现最简单的业务代码
        //2. 添加到t_employee表
        employeeMapper.save(dto.getEmployee());
        //adminId 不能为空 设置adminId
        dto.getShop().setAdminId(dto.getEmployee().getId());
        //1. 添加到t_shop表
        shopMapper.save(dto.getShop());
        return AjaxResult.me();
    }
}
